package com.xmall.service.impl;

import com.xmall.bean.table.*;
import com.xmall.bean.vo.AddressDataVo;
import com.xmall.mapper.AddressMapper;
import com.xmall.mapper.RegionMapper;
import com.xmall.mapper.UserMapper;
import com.xmall.service.AddressService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.System;
import java.util.*;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    RegionMapper regionMapper;
    @Autowired
    UserMapper userMapper;


    @Override
    public List<AddressDataVo> showAddressList() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        Integer id = user.getId();
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteriaAddress = addressExample.createCriteria();
        if(id != null){
            criteriaAddress.andUserIdEqualTo(id);
        }
        criteriaAddress.andDeletedEqualTo(false);

        List<Address> addresses = addressMapper.selectByExample(addressExample);
        List<AddressDataVo> addressDataVos = new ArrayList<>();
        for (Address address : addresses) {
            Integer provinceId = address.getProvinceId();
            Integer cityId = address.getCityId();
            Integer areaId = address.getAreaId();
            Region regionProvince = regionMapper.selectByPrimaryKey(provinceId);
            Region regionCity = regionMapper.selectByPrimaryKey(cityId);
            Region regionArea = regionMapper.selectByPrimaryKey(areaId);
            StringBuilder stringBuilder = new StringBuilder();
            StringBuilder builder = stringBuilder.append(regionProvince.getName()).append(regionCity.getName()).append(regionArea.getName());
            String detailedAddress = builder.toString();
            AddressDataVo addressDataVo = new AddressDataVo(detailedAddress,address.getId(),address.getIsDefault(),address.getMobile(),address.getName());
            addressDataVos.add(addressDataVo);
        }

        return addressDataVos;
    }

    @Override
    public Map<String, Object> showDetailById(Integer id) {
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        if(id != null){
            criteria.andIdEqualTo(id);
        }
        criteria.andDeletedEqualTo(false);
        List<Address> addresses = addressMapper.selectByExample(addressExample);
        Address address = addresses.get(0);
        Integer provinceId = address.getProvinceId();
        Integer cityId = address.getCityId();
        Integer areaId = address.getAreaId();
        Region regionProvince = regionMapper.selectByPrimaryKey(provinceId);
        Region regionCity = regionMapper.selectByPrimaryKey(cityId);
        Region regionArea = regionMapper.selectByPrimaryKey(areaId);
        Map<String,Object> map = new HashMap<>();
        map.put("address",address.getAddress());
        map.put("areaId",address.getAreaId());
        map.put("areaName",regionArea);
        map.put("cityId",address.getCityId());
        map.put("cityName",regionCity);
        map.put("id",address.getId());
        map.put("isDefault",address.getIsDefault());
        map.put("mobile",address.getMobile());
        map.put("name",address.getName());
        map.put("provinceId",address.getProvinceId());
        map.put("provinceNam",regionProvince);
        return map;
    }

    @Override
    public Integer saveAddressDetail(Address address) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        if (address.getId() == 0) {
            address.setId(null);
            long l = System.currentTimeMillis();
            Date date = new Date(l);
            address.setAddTime(date);
            address.setUpdateTime(date);
            address.setUserId(user.getId());
            if (address.getIsDefault() == false) {
                addressMapper.insertSelective(address);
                Integer id = address.getId();
                return id;
            }
                changeDefaultStatus(user.getId());
                addressMapper.insertSelective(address);
                return address.getId();

        } else
            if (address.getIsDefault() == true) {
                changeDefaultStatus(user.getId());
                criteria.andIdEqualTo(address.getId());
                int i = addressMapper.updateByExampleSelective(address, addressExample);
                return address.getId();
            }
            criteria.andIdEqualTo(address.getId());
                addressMapper.updateByExampleSelective(address, addressExample);
                return address.getId();
        }


    @Override
    public void deleteAddressDetail(Integer id) {
        AddressExample addressExample =  new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        if(id != null){
            criteria.andIdEqualTo(id);
        }
        Address address = new Address();
        address.setDeleted(true);
        addressMapper.updateByExampleSelective(address,addressExample);
    }


    public void changeDefaultStatus(Integer userId){
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andIsDefaultEqualTo(true);
        List<Address> addresses = addressMapper.selectByExample(addressExample);
        if (addresses.size() == 0){
            return;
        }
        Address address = addresses.get(0);
        Integer id = address.getId();
        Boolean isDefault = false;
        addressMapper.updateById(isDefault,id);

    }

}
